﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYRotateElements : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			if (commandData == null)
			{
				throw new System.ArgumentNullException();
			}
			UIApplication application = commandData.Application;
			Document document = application.ActiveUIDocument.Document;
			Selection selection = application.ActiveUIDocument.Selection;
			List<ViewType> list = new List<ViewType>();
            list.Add((ViewType)1);
            list.Add((ViewType)2);
            list.Add((ViewType)4);
			if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FormRotateEntiy formRotateEntiy = new FormRotateEntiy();
			Result result;
			try
			{
				string text = "请框选要旋转的实体集合，其中必须包含一个族实例，且只可有一个族实例……";
				YJKWindowHandle owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				formRotateEntiy.Show(owner);
				while (!formRotateEntiy.IsDisposed)
				{
					transaction = new Transaction(document, "45°旋转实体");
					IList<Element> list2 = new List<Element>();
					if (selection.GetElementIds().Count > 0)
					{
						IEnumerator<ElementId> enumerator = selection.GetElementIds().GetEnumerator();
						{
							while (enumerator.MoveNext())
							{
								ElementId elementId = enumerator.Current;
								list2.Add(document.GetElementById(elementId));
							}
							goto IL_119;
						}
					}
					list2 = selection.PickElementsByRectangle(text);
					if (formRotateEntiy.IsDisposed)
					{
						break;
					}
					if (list2.Count <= 0)
					{
						YJKMessageBox.Show("未选中旋转实体，\n请重新选择实体");
						continue;
					}
					IL_119:
					transaction.Start();
					YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance(list2);
					double rotateAngle = formRotateEntiy.rotateAngle;
					string text2 = hyrotateFamilyInstance.RotateElem(document, rotateAngle);
					RevitVersionFuncs.ClearSelection(selection);
					if (!string.IsNullOrEmpty(text2))
					{
						YJKMessageBox.Show(text2);
					}
					transaction.Commit();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				if (!formRotateEntiy.IsDisposed)
				{
					formRotateEntiy.Close();
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null)
				{
					TransactionStatus status = transaction.GetStatus();
					if ((int)status == 1)
					{
						transaction.Commit();
					}
					else if ((int)status == 5)
					{
						transaction.RollBack();
					}
				}
			}
			return result;
		}
	}
}
